package com.jiutong.meipengdai.ui.activitys;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.base.CommonAdapter;
import com.jiutong.meipengdai.base.ViewHolder;
import com.jiutong.meipengdai.entity.GatheringListRespond;
import com.jiutong.meipengdai.http.model.interfaces.BaseBiz;
import com.jiutong.meipengdai.http.model.interfaces.CheckLoginInterface;
import com.jiutong.meipengdai.http.present.GatheringPresent;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的收款
 * Created by suxi on 2017/6/21.
 */


@CheckLoginInterface
public class GatheringActivity extends BaseActivity implements BaseBiz {
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_detail)
    TextView tvDetail;
    @Bind(R.id.tv_total_gathering)
    TextView tvTotalGathering;
    @Bind(R.id.tv_no_date)
    TextView tvNoDate;
    @Bind(R.id.view_gray_line)
    View viewLine;
    @Bind(R.id.listview_gathering)
    ListView listviewGathering;
    private GatheringPresent gatheringPresent;
    private DecimalFormat decimalFormat;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_gathering;
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        tvTitle.setText("我的收款");
        gatheringPresent = new GatheringPresent(mContext, this);
        gatheringPresent.getGatheringList();
    }

    @OnClick(R.id.rl_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onSuccess(Object object) {
        GatheringListRespond respond = (GatheringListRespond) object;
        if("200".equals(respond.getResCode())){

            tvTotalGathering.setText(respond.getData().getReceivablesSum());
            List<GatheringListRespond.DataBean.CreditRecordPojoBean> creditRecordPojo = respond.getData().getCreditRecordPojo();

            if(creditRecordPojo != null && creditRecordPojo.size() > 0){
                tvNoDate.setVisibility(View.GONE);
                viewLine.setVisibility(View.VISIBLE);
                tvDetail.setVisibility(View.VISIBLE);

                GatheringAdapter gatheringAdapter = new GatheringAdapter(mContext, creditRecordPojo, R.layout.view_gathering_item);
                listviewGathering.setAdapter(gatheringAdapter);

                decimalFormat = new DecimalFormat(".00");
                if (!TextUtils.isEmpty(respond.getData().getReceivablesSum())) {
                    tvTotalGathering.setText(Float.parseFloat(respond.getData().getReceivablesSum()) >= 10000 ? decimalFormat.format(Float.parseFloat(respond.getData().getReceivablesSum()) / 10000) + "万" : respond.getData().getReceivablesSum());
                }
            }
        }
    }

    @Override
    public void onError(Throwable e) {

    }

    public class GatheringAdapter extends CommonAdapter<GatheringListRespond.DataBean.CreditRecordPojoBean> {

        public GatheringAdapter(Context context, List<GatheringListRespond.DataBean.CreditRecordPojoBean> listDatas, int layoutId) {
            super(context, listDatas, layoutId);
        }

        @Override
        protected void fillData(ViewHolder holder, int position) {
            TextView tvDesc = holder.getView(R.id.tv_desc);
            TextView tvMoney = holder.getView(R.id.tv_money);
            TextView tvTime = holder.getView(R.id.tv_time);
            tvDesc.setText(listDatas.get(position).getRemarks());

            if (!TextUtils.isEmpty(listDatas.get(position).getActMoney()))
                tvMoney.setText("+  " + (Float.parseFloat(listDatas.get(position).getActMoney()) >= 10000 ? decimalFormat.format(Float.parseFloat(listDatas.get(position).getActMoney()) / 10000) + "万" : listDatas.get(position).getActMoney()));

            tvTime.setText(listDatas.get(position).getCreditDate());
        }
    }
}
